<?php

namespace System\Registry;

use System\Registry\Exception\SetKeyException;
use DOMDocument;

/**
 * 注册表类 该功能是DreamFramework框架独有的功能。
 * 
 * @package Dream
 * @subpackage Regedit
 * @namespace System\Registry
 * @author lewis (lewisld@yeah.net)
 */
class Regedit
{

    /**
     * 创建一张注册表
     * 
     * @static
     *
     * @access public
     * @param string $table 注册表名
     * @param array $param 参数
     * @return void 没有返回值
     */
    public static function create($table, $param = [])
    {
        if (isset($param['encoding']) && ! empty($param['encoding'])) {
            $encoding = strtoupper($param['encoding']);
        } else {
            $encoding = 'UTF-8';
        }
        if (isset($param['version']) && ! empty($param['version'])) {
            $version = $param['version'];
        } else {
            $version = '1.0';
        }
        $table = ucwords(strtolower($table));
        $file = REGISTRY_PATH . DS . $table . '.xml';
        $dom = new DOMDocument();
        $dom->formatOutput = TRUE;
        $dom->version = $version;
        $dom->encoding = $encoding;
        $dom->appendChild($dom->createElement('registry', ''));
        $dom->save($file);
        unset($encoding, $version, $table, $file, $dom);
    }

    /**
     * 设置注册表的键值对
     * 
     * @static
     *
     * @access public
     * @param string $name 名称（简单的格式：表名.项.键名）
     * @param mixed $value 键值
     * @return
     *
     */
    public static function set($name, $value)
    {
        $arr = explode('.', $name);
        $table = $arr[0]; // 获取表明
        $key = array_pop($arr[1]); // 获键名
        $item = $arr[1]; // 获取项
        $count = count($item);
        $file = REGISTRY_PATH . DS . $table . '.xml'; // 组装注册表XML文件地址
        $dom = new DOMDocument();
        $dom->formatOutput = TRUE;
        $dom->load($file);
        $data = $dom->getElementsByTagName('registry');
        for ($i = 0; $i < $count; $i ++) {
            $data = $dom->getElementsByTagName($item[$i]);
        }
        $tag = $dom->createElement('key');
        $content = $dom->createTextNode($value);
        $dom->setAttribute('name', $name);
        $dom->appendChild($content);
        $data->item(0)->appendChild($tag);
        $dom->save($file);
        unset($arr, $table, $key, $item, $count, $file, $dom, $data, $tag, $content);
        if (0 < SetKeyException::getCode()) {
            throw new SetKeyException([
                [
                    'system.registry.set.key.exception'
                ],
                [
                    'name' => $name,
                    'value' => $value
                ]
            ]);
        }
        return TRUE;
    }

    public static function get($name, $default = NULL)
    {
        $arr = explode('.', $name);
        $table = $arr[0]; // 获取表名
        $key = array_pop($arr[1]); // 获键名
        $item = $arr[1]; // 获取项
        $count = count($item);
        $file = REGISTRY_PATH . DS . $table . '.xml'; // 组装注册表XML文件地址
        $dom = new DOMDocument();
        $dom->formatOutput = TRUE;
        $dom->load($file);
        $tag = $dom->getElementsByTagName('registry');
        for ($i = 0; $i < $count; $i ++) {
            $tag = $dom->getElementsByTagName($item[$i]);
        }
        $tag = $dom->getElementsByTagName('key');
        foreach ($tag as $att) {
            if ($key === $att->getAttribute('name')) {
                return $att->nodeValue;
            }
        }
        unset($arr, $table, $key, $item, $count, $file, $dom, $tag, $att);
        return $default;
    }
}
